package com.wdb;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author 王德斌
 *
 */
public class Server {
    public static void main(String[] args) throws IOException {
        DataInputStream dis;
        DataOutputStream dos;
        File file = new File("");
        String filePath = file.getCanonicalPath();
        System.out.println("------server------");
        //1.指定端口：使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(9999);
        //2.阻塞式等待连接accept
        while (true) {
            Socket client = server.accept();
            System.out.println("一个客户端建立了连接");
            dis = new DataInputStream(client.getInputStream());
            dos = new DataOutputStream(client.getOutputStream());
            try {
                String str = dis.readUTF();
                if (str.equals("0000")) {//0000代表字符传转文件
                    String msg = "success";
                    String in = dis.readUTF();
                    String out = dis.readUTF();
                    String id = dis.readUTF();
                    try {
                        creatData(in, out, id);
                    } catch (IOException e) {
                        msg = "error";
                    }
                    dos.writeUTF(msg);
                    dos.flush();
                } else if (str.equals("1111")) {//1111代表导入的是文件

                }
            } catch (IOException e) {
                System.out.println("--------读写异常-------");
            } finally {
                dos.writeUTF("over");
                dos.flush();
                close(dis, dos, client);
            }
        }
    }

    public static void close(Closeable... targets) {
        for (Closeable target : targets) {
            try {
                if (target != null) {
                    target.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void creatData(String in, String out, String id) throws IOException {
        String[] ins = in.split("\\r?\\n");
        String[] outs = out.split("\\r?\\n");
        File file = new File("");
        String filePath = file.getCanonicalPath();
        filePath += "//judge//data//" + id;
        file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        for (int n = 0; n < ins.length; n++) {
            ins[n] = ins[n].replace(" ", "");
            ins[n] = ins[n].replace(",", "\n");
            file = new File(filePath + "//" + n + ".in");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(ins[n].getBytes());
        }
        for (int n = 0; n < outs.length; n++) {
            outs[n] = outs[n].replace(" ", "");
            outs[n] = outs[n].replace(",", "\n");
            file = new File(filePath + "//" + n + ".out");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(outs[n].getBytes());
        }
    }
}